#include "small_message.h"
#include "ui_small_message.h"
#include "QPainterPath"
#include "qpainter.h"
#include "QMenu"
#include "QMessageBox"
#include <QDebug>

small_message::small_message(QString name, QPixmap picture, QWidget* parent) :
    QWidget(parent),
    ui(new Ui::small_message)
{
    ui->setupUi(this);
    ui->setupUi(this);
    set_name(name);
    set_picture(picture);
    ui->widget_page->installEventFilter(this);    // 安装事件过滤器
    setMouseTracking(true); //鼠标追踪
}

small_message::~small_message()
{
    delete ui;
}

//头像显示
void small_message::set_picture(QPixmap pixmap)
{
    pixmap = pixmap.scaled(ui->label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    int width = ui->label->size().width();
    int height = ui->label->size().height();

    QPixmap image(width, height);
    image.fill(Qt::transparent);

    QPainterPath painterPath;
    painterPath.addEllipse(0, 0, width, height);

    QPainter painter(&image);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setClipPath(painterPath);
    painter.drawPixmap(0, 0, width, height, pixmap);
    //绘制到label
    ui->label->setPixmap(image);
}

//对方用户昵称显示
void small_message::set_name(QString name) {
    ui->label_name->setText(name);
}

bool small_message::eventFilter(QObject* obj, QEvent* event)
{
    if (obj == ui->widget_page) {
        //悬浮，变深
        if (event->type() == QEvent::Enter) {
            // 当鼠标进入时，将背景颜色变深
            QPalette palette = ui->widget_page->palette();
            palette.setColor(QPalette::Window, QColor(200, 200, 200));
            ui->widget_page->setAutoFillBackground(true);
            ui->widget_page->setPalette(palette);
            ui->widget_page->update(); // 更新以显示新的背景颜色
            return true;
        }
        //悬浮，恢复
        else if (event->type() == QEvent::Leave) {
            // 当鼠标离开时，恢复原来的背景颜色
            QPalette palette = ui->widget_page->palette();
            palette.setColor(QPalette::Window, Qt::transparent);
            ui->widget_page->setAutoFillBackground(true);
            ui->widget_page->setPalette(palette);
            ui->widget_page->update(); // 更新以显示新的背景颜色
            return true;
        }
        //单击
        else if (event->type() == QEvent::MouseButtonPress) {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            // 在左键点击时，打开聊天框
            qDebug() << "2dudu";
            if (mouseEvent->button() == Qt::LeftButton) {
                if (chat_window && (!chat_window->visibleRegion().isEmpty())) {
                    qDebug() << "2弹出原聊天框";
                    chat_window->activateWindow();
                    chat_window->setWindowState((chat_window->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
                    return true;
                }
                else {
                    qDebug() << "2产生新聊天框";
                    //chat_window = new chatwindow();
                    chat_window->show();
                    return true;
                }
                qDebug() << "2犯病了";

            }
            // 在右键点击时弹出选择窗口
            else if (mouseEvent->button() == Qt::RightButton) {

                QPoint pos = mapToGlobal(mouseEvent->pos());
                QMenu contextMenu;
                QAction action("用户资料", this);
                connect(&action, &QAction::triggered, [this]() {
                    // 处理选择窗口的操作
                    QMessageBox::information(this, "查看资料", "这是一个资料");
                    });
                contextMenu.addAction(&action);
                contextMenu.exec(pos);
                return true;
            }
        }
        else {
            return false;
        }
    }
    return QWidget::eventFilter(obj, event);
}
